//
//  PlayCountDownView.m
//  GC_OC
//
//  Created by wlf on 2022/5/16.
//

#import "PlayCountDownView.h"
#import "PlayCountDownTableViewCell.h"
@implementation PlayCountDownView

- (void)showIn:(UIView *)view muliter:(NSString *)countDownStr{
    [view addSubview:self];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];
    for (int i = 0; i<self.dataSource.count; i++) {
        NSString *str = self.dataSource[i];
        if ([str isEqualToString:countDownStr]) {
            self.index = i;
            
            [self.mulitTabView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
            break;
        }
    }
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeViewWithTap)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458)];
        self.contentView = contentView;
        contentView.backgroundColor = [UIColor whiteColor];
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH, 1000) byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
         CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
         maskLayer.frame = contentView.bounds;
         maskLayer.path = maskPath.CGPath;
        contentView.layer.mask = maskLayer;
        [self addSubview:contentView];
    
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(31, 14, 80, 22)];
        titleLabel.text = @"定时关闭";
        titleLabel.font = [UIFont boldSystemFontOfSize: 16];
        titleLabel.textColor = [UIColor blackColor];
        [contentView addSubview:titleLabel];
        
        UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 19, SCREEN_WIDTH - 110, 17)];
        subTitleLabel.text = @"（计时结束后，将暂停播放歌曲）";
        subTitleLabel.font = [UIFont systemFontOfSize: 12];
        subTitleLabel.textColor = [UIColor blackColor];
        [contentView addSubview:subTitleLabel];
        
        self.mulitTabView.frame = CGRectMake(0, 36, SCREEN_WIDTH, 458 - 36);
        [contentView addSubview:self.mulitTabView];
        [self.mulitTabView reloadData];
    }
    return self;
}
- (void)closeViewWithTap{
    
    [UIView animateWithDuration:0.3 animations:^{
           
           self.contentView.transform = CGAffineTransformMakeTranslation(0, 458);
       } completion:^(BOOL finished) {
           
           [self removeFromSuperview];
           [self.contentView removeFromSuperview];
           
       }];
}
- (void)setCountTime:(NSString *)time{
    PlayCountDownTableViewCell *cell = [self.mulitTabView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0]];
    cell.subTitleLab.text = time;
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint point = [touch locationInView:self];
    if (point.y <= CGRectGetMinY(self.contentView.frame)) {
        
        return YES;
    }else{
        return NO;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayCountDownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Mulitple" forIndexPath:indexPath];
    NSString *title = self.dataSource[indexPath.row];
    cell.titleLab.text = title;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.index = indexPath.row;
    if (self.countDownIndexBlock) {
        self.countDownIndexBlock(indexPath.row);
    }
}
- (UITableView *)mulitTabView{
    if (!_mulitTabView) {
        _mulitTabView = [[UITableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458) style:UITableViewStylePlain];
        _mulitTabView.dataSource = self;
        _mulitTabView.delegate = self;
        _mulitTabView.showsVerticalScrollIndicator = NO;
        [_mulitTabView registerClass:[PlayCountDownTableViewCell class] forCellReuseIdentifier:@"Mulitple"];
        _mulitTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _mulitTabView;
}

@end
